﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
 ●版本：1.0
**********************************************************/
using System;
using System.Text;
using Smartkernel.Exceptions;
using Smartkernel.Extensions;
using Smartkernel.Resource;

namespace Smartkernel.Attributes
{
    /// <summary>
    /// XML文件中节点与实体对象字段或属性的映射标识类
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public partial class SmartXmlMappingAttribute : Attribute
    {
        #region 字段和属性
        private string nodeName;
        /// <summary>
        /// 映射到XML文件中的节点，不区分大小写
        /// </summary>
        public string NodeName
        {
            get
            {
                return this.nodeName;
            }
        }
        private object defaultValue;
        /// <summary>
        /// 如果XML文件中没有对应的值时，所赋给实体字段或属性的默认值，如果没有设置默认值，则取字段或属性初始化时的默认值
        /// </summary>
        public object DefaultValue
        {
            get
            {
                return this.defaultValue;
            }
            set
            {
                this.defaultValue = value;
            }
        }
        #endregion
    }
    public partial class SmartXmlMappingAttribute
    {
        #region 构造函数和方法
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="nodeName">映射到XML文件中的节点，不区分大小写</param>
        public SmartXmlMappingAttribute(string nodeName)
        {
            if (nodeName.IsNullOrEmptyOrBlank())
            {
                throw new SmartException(SmartExceptionMessageResource.SmartXmlMappingAttribute_nodeName_NullOrEmptyOrBlank);
            }

            this.nodeName = nodeName.Trim();
        }
        #endregion
    }
    public partial class SmartXmlMappingAttribute
    {
        #region 覆盖基类的方法
        /// <summary>
        /// 比较两个对象是否相等
        /// </summary>
        /// <param name="obj">要比较的对象</param>
        /// <returns>比较的结果</returns>
        public override bool Equals(object obj)
        {
            SmartXmlMappingAttribute other = obj as SmartXmlMappingAttribute;
            if (other == null)
            {
                return false;
            }
            else
            {
                return this.NodeName == other.NodeName && this.DefaultValue == other.DefaultValue;
            }
        }
        /// <summary>
        /// 获得哈希码
        /// </summary>
        /// <returns>哈希码</returns>
        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
        /// <summary>
        /// 获得字符串表示
        /// </summary>
        /// <returns>输出的字符串</returns>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder(50);
            stringBuilder.Append("SmartXmlMappingAttribute");
            if (this.NodeName != null)
            {
                stringBuilder.Append(this.NodeName.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.DefaultValue != null)
            {
                stringBuilder.Append(this.DefaultValue.GetHashCode().ToString());
            }
            return stringBuilder.ToString();
        }
        #endregion
    }
}
